{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出9x9乘法口诀表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1=1\t\n",
      "1x2=2\t2x2=4\t\n",
      "1x3=3\t2x3=6\t3x3=9\t\n",
      "1x4=4\t2x4=8\t3x4=12\t4x4=16\t\n",
      "1x5=5\t2x5=10\t3x5=15\t4x5=20\t5x5=25\t\n",
      "1x6=6\t2x6=12\t3x6=18\t4x6=24\t5x6=30\t6x6=36\t\n",
      "1x7=7\t2x7=14\t3x7=21\t4x7=28\t5x7=35\t6x7=42\t7x7=49\t\n",
      "1x8=8\t2x8=16\t3x8=24\t4x8=32\t5x8=40\t6x8=48\t7x8=56\t8x8=64\t\n",
      "1x9=9\t2x9=18\t3x9=27\t4x9=36\t5x9=45\t6x9=54\t7x9=63\t8x9=72\t9x9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):  # 外层循环控制行数\n",
    "    for j in range(1, i + 1):  # 内层循环控制列数\n",
    "        print(f\"{j}x{i}={i*j}\", end=\"\\t\")  # 打印乘法表达式，并用制表符分隔\n",
    "    print()  # 每完成一行输出后换行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "85=5*17\n"
     ]
    }
   ],
   "source": [
    "def prime_factors(n):\n",
    "    i = 2\n",
    "    factors = []\n",
    "    while i * i <= n:\n",
    "        if n % i:\n",
    "            i += 1\n",
    "        else:\n",
    "            n //= i\n",
    "            factors.append(i)\n",
    "    if n > 1:\n",
    "        factors.append(n)\n",
    "    return factors\n",
    "\n",
    "num = int(input(\"请输入一个正整数: \"))\n",
    "result = prime_factors(num)\n",
    "print(f\"{num}=\" + \"*\".join(map(str, result)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个4位数。\n",
      "逆序打印各位数字为: 7451\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入\n",
    "num = input(\"请输入一个不多于6位的正整数: \")\n",
    "\n",
    "# 确保输入的是正整数且不超过6位\n",
    "if num.isdigit() and len(num) <= 6:\n",
    "    # 计算位数\n",
    "    digits_count = len(num)\n",
    "    print(f\"这是一个{digits_count}位数。\")\n",
    "    \n",
    "    # 逆序打印各位数字\n",
    "    reversed_num = num[::-1]\n",
    "    print(\"逆序打印各位数字为:\", reversed_num)\n",
    "else:\n",
    "    print(\"输入错误，请输入一个不多于6位的正整数。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是: 617280\n"
     ]
    }
   ],
   "source": [
    "def sum_of_repeated_numbers(a, n):\n",
    "    total = 0\n",
    "    num_str = \"\"\n",
    "    for i in range(n):\n",
    "        num_str += str(a)\n",
    "        total += int(num_str)\n",
    "    return total\n",
    "\n",
    "# 获取用户输入\n",
    "a = int(input(\"请输入数字a: \"))\n",
    "n = int(input(\"请输入要累加的次数n: \"))\n",
    "\n",
    "# 计算结果并打印\n",
    "result = sum_of_repeated_numbers(a, n)\n",
    "print(f\"结果是: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数列的前5项之和为: 7.7666666666666675\n"
     ]
    }
   ],
   "source": [
    "def fibonacci(n):\n",
    "    fib = [0, 1]\n",
    "    for i in range(2, n + 1):\n",
    "        fib.append(fib[-1] + fib[-2])\n",
    "    return fib\n",
    "\n",
    "def sum_of_series(N):\n",
    "    fib = fibonacci(N + 1)\n",
    "    total_sum = 0\n",
    "    for i in range(1, N + 1):\n",
    "        total_sum += fib[i + 1] / fib[i]\n",
    "    return total_sum\n",
    "\n",
    "# 从键盘输入N\n",
    "N = int(input(\"请输入N的值: \"))\n",
    "result = sum_of_series(N)\n",
    "print(f\"该数列的前{N}项之和为: {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "满足条件的整数是: 4256\n"
     ]
    }
   ],
   "source": [
    "# 定义一个函数来检查一个数是否是完全平方数\n",
    "def is_perfect_square(n):\n",
    "    root = int(n ** 0.5)\n",
    "    return root * root == n\n",
    "\n",
    "# 遍历可能的整数，找到满足条件的数\n",
    "for x in range(10**6):  # 假设整数不会太大，这里取一个合理的上限\n",
    "    if is_perfect_square(x + 100) and is_perfect_square(x + 100 + 268):\n",
    "        print(f\"满足条件的整数是: {x}\")\n",
    "        break  # 找到答案后退出循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n"
     ]
    }
   ],
   "source": [
    "def rabbit_count(months):\n",
    "    if months == 1 or months == 2 or months == 3:\n",
    "        return 1\n",
    "    else:\n",
    "        a, b = 1, 1\n",
    "        for i in range(4, months + 1):\n",
    "            a, b = b, a + b\n",
    "        return b\n",
    "\n",
    "# 测试代码\n",
    "for month in range(1, 13):\n",
    "    print(f\"第{month}个月的兔子总数为：{rabbit_count(month)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意输入一个3x3矩阵，求矩阵对角线元素之和。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对角线元素之和为: 16\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 创建一个3x3的矩阵\n",
    "matrix = np.array([[2, 2, 3],\n",
    "                   [4, 5, 6],\n",
    "                   [7, 7, 9]])\n",
    "\n",
    "# 获取对角线元素\n",
    "diagonal_elements = np.diagonal(matrix)\n",
    "\n",
    "# 计算对角线元素之和\n",
    "sum_of_diagonal = np.sum(diagonal_elements)\n",
    "\n",
    "print(\"对角线元素之和为:\", sum_of_diagonal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 9, 10, 1, 2, 3, 4, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "def rotate_list(lst, n, m):\n",
    "    # 检查输入的有效性\n",
    "    if n > len(lst) or m > len(lst):\n",
    "        return \"n或m超出列表长度\"\n",
    "    \n",
    "    # 使用切片操作进行旋转\n",
    "    result = lst[-m:] + lst[:-m]\n",
    "    return result\n",
    "\n",
    "# 示例列表\n",
    "example_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "# 调用函数，将前10个元素向后移动3个位置\n",
    "rotated_list = rotate_list(example_list, 10, 3)\n",
    "print(rotated_list)  # 输出: [8, 9, 10, 1, 2, 3, 4, 5, 6, 7]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果是: 1.5333333333333332\n"
     ]
    }
   ],
   "source": [
    "# 定义计算偶数序列和的函数\n",
    "def sum_even_series(n):\n",
    "    return sum(1 / i for i in range(2, n + 1, 2))\n",
    "\n",
    "# 定义计算奇数序列和的函数\n",
    "def sum_odd_series(n):\n",
    "    return sum(1 / i for i in range(1, n + 1, 2))\n",
    "\n",
    "# 主函数，根据n的奇偶性调用相应的计算函数\n",
    "def calculate_series(n):\n",
    "    # 使用函数作为变量（模拟函数指针）\n",
    "    if n % 2 == 0:\n",
    "        series_sum_func = sum_even_series\n",
    "    else:\n",
    "        series_sum_func = sum_odd_series\n",
    "    \n",
    "    # 调用函数并返回结果\n",
    "    return series_sum_func(n)\n",
    "\n",
    "# 测试主函数\n",
    "if __name__ == \"__main__\":\n",
    "    n = int(input(\"请输入一个整数n: \"))\n",
    "    result = calculate_series(n)\n",
    "    print(f\"结果是: {result}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
